package com.mine.controller;

import com.mine.pojo.OrderItem;
import com.mine.pojo.User;
import com.mine.service.CartService;
import com.mine.service.MealService;
import com.mine.service.OrderService;
import com.mine.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    MealService mealService;
    @Autowired
    UserService userService;
    @Autowired
    CartService cartService;
    @Autowired
    OrderService orderService;

    //*展示用户的购物车
    @GetMapping("/cartList")
    public String cart(Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user==null){ //如果找不到用户，转到登录页
            model.addAttribute("msg","请先登录");
            return "login";
        }
        float num = 0.0f;
        List<OrderItem> orderItemList = orderService.queryAllOrderItemByUser(user);
        if(orderItemList != null){
            for (OrderItem orderItem : orderItemList) {
                num = num + (orderItem.getMeal().getMeal_price_now().floatValue() * orderItem.getNum());
            }
        }
        model.addAttribute("orderItemList",orderItemList);
        model.addAttribute("num",num);
        return "meal/cart";
    }
    //*修改购物车数量
    @GetMapping("/minmeal")
    @ResponseBody
    public String minmeal(Integer meal_id, Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        orderService.reduceOrderItemNum(meal_id,user.getUser_id());
        float num = 0.0f;
        List<OrderItem> orderItemList = orderService.queryAllOrderItemByUser(user);
        if(orderItemList != null){
            for (OrderItem orderItem : orderItemList) {
                num = num + (orderItem.getMeal().getMeal_price_now().floatValue() * orderItem.getNum());
            }
        }
        model.addAttribute("orderItemList",orderItemList);
        model.addAttribute("num",num);
        return "OK";
    }
    @GetMapping("/addmeal")
    @ResponseBody
    public String addmeal(Integer meal_id, Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        orderService.addOrderItemNum(meal_id,user.getUser_id());
        float num = 0.0f;
        List<OrderItem> orderItemList = orderService.queryAllOrderItemByUser(user);
        if(orderItemList != null){
            for (OrderItem orderItem : orderItemList) {
                num = num + (orderItem.getMeal().getMeal_price_now().floatValue() * orderItem.getNum());
            }
        }
        model.addAttribute("orderItemList",orderItemList);
        model.addAttribute("num",num);
        return "OK";
    }
    //*删除购物车物品
    @GetMapping("/deletemealcart")
    @ResponseBody
    public String deletemealcart(Integer meal_id, Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        orderService.deletemealcart(meal_id,user.getUser_id());
        float num = 0.0f;
        List<OrderItem> orderItemList = orderService.queryAllOrderItemByUser(user);
        if(orderItemList != null){
            for (OrderItem orderItem : orderItemList) {
                num = num + (orderItem.getMeal().getMeal_price_now().floatValue() * orderItem.getNum());
            }
        }
        model.addAttribute("orderItemList",orderItemList);
        model.addAttribute("num",num);
        return "OK";
    }
}
